import { getBaseUrl } from '@/services/urlconst';
import { getLoadMapList, getSelectDataMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useCallback, useMemo } from 'react';
import { useRequest } from 'umi';
import { AcceptanceManageAPI } from '../common/type';
import { getSearchColumns } from '../mconfig';
import { DictionaryService } from './DictionaryService';
import { ApplyManageTab } from '../mconfig/ManageTabSwitch';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang'; // 本地环境

const AcceptanceTableManage = (curRes: Record<string, any>) => {
  const { materlelStatusList, assetBrandType, assetSourceType, loadMdicIds } = DictionaryService();

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = ApplyManageTab(curRes);

  /** 获取搜索参数 */
  // const searchListParams = ManageSearchColumns(loadMdicMapObj?.loadMapInfo, queryTypeObj);

  /***保存当前的搜索参数**/
  // const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /** 获取列表数据 */
  const { tableProps, data, error, loading, run, params } = useRequest(
    (opt: AcceptanceManageAPI.IGridRequestType) => {
      let personUrl = `${baseUrl}/assetMaterial/list/${queryTypeObj?.params}`;
      let personBody: any = {
        ...queryTypeObj,
        order: 'desc',
        page: 1,
        rows: 10,
        sort: 'createTime',
        dicTypeIds: loadMdicIds?.toString(), // 字典入参
        ...opt,
      };
      return requestFunComponFiex('POST', personUrl, personBody);
    },
    {
      manual: true, //页面初始请求为true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getAcceptanceTableData = useCallback((otherRequest: AcceptanceManageAPI.IGridRequestType | any) => {
    const request: AcceptanceManageAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    run({
      ...request,
    });
  }, []);

  /* 页面列表 */
  let tableValues = useMemo(() => {
    return {
      acceptanceData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /* 数据字典值  */
  let loadMapList = useMemo(() => {
    return {
      loadMdicMapList: [],
      searchList: getSearchColumns({ materlelStatusList, assetSourceType, assetBrandType }),
    };
  }, [params]);

  return {
    loading,
    getAcceptanceTableData, //其他请求操作方法例如刷新等等
    // manageTableParams,
    otherParams: params?.[0],
    ...tableValues, //页面列表值
    ...loadMapList,
  };
};

export { AcceptanceTableManage };
